package xuecheng.tool.mq.redis;

import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import xuecheng.tool.json.JsonUtils;
import xuecheng.tool.json.JsonUtilsFactory;
import xuecheng.tool.nosql.redis.RedisUtils;

/**
 * redis 队列工具类
 *
 * @author PengXueCheng
 * @version 1.0.0
 */
@Slf4j
public class RedisMqUtils {

    private RedisUtils redisUtils;

    private JsonUtils jsonUtils = JsonUtilsFactory.obtain();

    public RedisMqUtils(RedisUtils redisUtils) {
        this.redisUtils = redisUtils;
    }

    /**
     * 发送消息
     *
     * @param topic
     *            队列主题
     * @param message
     *            消息实体
     * @param <T>
     */
    public <T> void send(@NonNull String topic, @NonNull T message) {
        send(topic, jsonUtils.toJson(message));
    }

    /**
     * 发送消息
     *
     * @param topic
     *            队列主题
     * @param messageJson
     *            消息 json 字符串
     */
    public void send(@NonNull String topic, @NonNull String messageJson) {
        Long insertCount = redisUtils.lRightPush(topic, messageJson);
        if (insertCount > 0) {
            log.info("Send Message success. topic = {}.", topic);
        } else {
            log.error("Send Message failed. topic = {}.", topic);
        }
    }

}
